con = require("../config")
local M = {}

local function find_project_root()
  local current_file = vim.fn.expand("%:p")
  if current_file == "" then
    current_file = vim.fn.getcwd()
  end
  local root_patterns = { "__nvim_project_root", ".git" }
  local root = vim.fs.dirname(vim.fs.find(root_patterns, {
    path = current_file,
    upward = true,
    stop = vim.loop.os_homedir(),
  })[1]) or vim.fn.getcwd()
  return root
end

local function choose_templates()
  local templates_dir = vim.fn.expand("~/.config/nvim/.vscode")
  local template_files = vim.fn.glob(templates_dir .. "/*", false, true)

  local template_names = {}
  for _, file in ipairs(template_files) do
    table.insert(template_names, vim.fn.fnamemodify(file, ":t"))
  end

  vim.ui.select(template_names, { prompt = "Choose a template:" },
  function (choice)
    if not choice then return end


    local current_dir = vim.fn.getcwd()
    local target_dir = current_dir .. "/.vscode"
    if vim.fn.isdirectory(target_dir) == 0 then
        vim.fn.mkdir(target_dir, "p")
    end

    local template_file = templates_dir .. "/" .. choice
    vim.fn.system(string.format("cp '%s' '%s'", template_file, target_dir))
    vim.notify("Template " .. choice .. ".json has been created!", vim.log.levels.INFO)
  end)
end

if conf.snack.picker then
M =  {
    {",gg", function ()
      local root = find_project_root()
      vim.cmd("cd " .. root)
      Snacks.lazygit()
    end, desc = "Lazygit"},
    { "<space>n",  function() Snacks.notifier.show_history() end, desc = "Notification History" },
    { "<space>un", function() Snacks.notifier.hide() end, desc = "Dismiss All Notifications" },
    { ",gB", function() Snacks.gitbrowse() end, desc = "Git Browse", mode = { "n", "v" } },
    { ",gl", function() Snacks.lazygit.log() end, desc = "Lazygit Log (cwd)" },
    { ",gb", function() Snacks.git.blame_line() end, desc = "Git Blame Line" },
    -- { "tt", function() Snacks.terminal.toggle() end, desc = "Toggle a terminal window" },
    { ",fj", function () choose_templates() end, "n", desc = "choose templates" },
  -- Top Pickers & Explorer
  -- { "<leader><space>", function() Snacks.picker.smart() end, desc = "Smart Find Files" },
  -- { ",fb", function() Snacks.picker.buffers() end, desc = "Buffers" },
  { ",fg", function()
      local root = find_project_root()
      vim.cmd("cd " .. root)
      Snacks.picker.grep()
    end, desc = "Grep" },
  { ",fc", function() Snacks.picker.command_history() end, desc = "Command History" },
  -- { "<leader>n", function() Snacks.picker.notifications() end, desc = "Notification History" },
  { ",t", function()
      local root = find_project_root()
      vim.cmd("cd " .. root)
      Snacks.explorer()
    end, desc = "File Explorer" },
  { ",e", function()
      local root = find_project_root()
      vim.cmd("cd " .. root)
      Snacks.explorer.reveal()
    end, desc = "File Explorer and Locate to the current file" },
  -- find
  { ",fb", function() Snacks.picker.buffers() end, desc = "Buffers" },
  { ",fk", function() Snacks.picker.files({ cwd = vim.fn.stdpath("config") }) end, desc = "Find Config File" },
  { ",ff", function()
      local root = find_project_root()
      vim.cmd("cd " .. root)
      Snacks.picker.files()
    end, desc = "Find Files" },
  { ",fl", function() Snacks.picker.git_files() end, desc = "Find Git Files" },
  { ",fp", function() Snacks.picker.projects() end, desc = "Projects" },
  { ",fr", function() Snacks.picker.recent() end, desc = "Recent" },
  { ",fdf", function() Snacks.picker.files({cwd = vim.fn.expand("%:p:h")}) end, desc = "Find Files in current directory" },
  { ",fdg", function() Snacks.picker.grep({cwd = vim.fn.expand("%:p:h")}) end, desc = "Grep in current directory" },
  -- git
  { "<space>gb", function() Snacks.picker.git_branches() end, desc = "Git Branches" },
  { "<space>gl", function() Snacks.picker.git_log() end, desc = "Git Log" },
  { "<space>gL", function() Snacks.picker.git_log_line() end, desc = "Git Log Line" },
  { "<space>gs", function() Snacks.picker.git_status() end, desc = "Git Status" },
  { "<space>gS", function() Snacks.picker.git_stash() end, desc = "Git Stash" },
  { "<space>gd", function() Snacks.picker.git_diff() end, desc = "Git Diff (Hunks)" },
  { "<space>gf", function() Snacks.picker.git_log_file() end, desc = "Git Log File" },
  -- Grep
  { ",sb", function() Snacks.picker.lines() end, desc = "Buffer Lines" },
  { ",sB", function() Snacks.picker.grep_buffers() end, desc = "Grep Open Buffers" },
  { ",sg", function() Snacks.picker.grep() end, desc = "Grep" },
  { ",sw", function() Snacks.picker.grep_word() end, desc = "Visual selection or word", mode = { "n", "x" } },
  -- search
  { '<space>s"', function() Snacks.picker.registers() end, desc = "Registers" },
  { '<space>s/', function() Snacks.picker.search_history() end, desc = "Search History" },
  { "<space>sa", function() Snacks.picker.autocmds() end, desc = "Autocmds" },
  { "<space>sb", function() Snacks.picker.lines() end, desc = "Buffer Lines" },
  { "<space>sc", function() Snacks.picker.command_history() end, desc = "Command History" },
  { "<space>sC", function() Snacks.picker.commands() end, desc = "Commands" },
  { "<space>sd", function() Snacks.picker.diagnostics() end, desc = "Diagnostics" },
  { "<space>sD", function() Snacks.picker.diagnostics_buffer() end, desc = "Buffer Diagnostics" },
  { "<space>sh", function() Snacks.picker.help() end, desc = "Help Pages" },
  { "<space>sH", function() Snacks.picker.highlights() end, desc = "Highlights" },
  { "<space>si", function() Snacks.picker.icons() end, desc = "Icons" },
  { "<space>sj", function() Snacks.picker.jumps() end, desc = "Jumps" },
  { "<space>sk", function() Snacks.picker.keymaps() end, desc = "Keymaps" },
  { "<space>sl", function() Snacks.picker.loclist() end, desc = "Location List" },
  { "<space>sm", function() Snacks.picker.marks() end, desc = "Marks" },
  { "<space>sM", function() Snacks.picker.man() end, desc = "Man Pages" },
  { "<space>sp", function() Snacks.picker.lazy() end, desc = "Search for Plugin Spec" },
  { "<space>sq", function() Snacks.picker.qflist() end, desc = "Quickfix List" },
  { "<space>sR", function() Snacks.picker.resume() end, desc = "Resume" },
  { "<space>su", function() Snacks.picker.undo() end, desc = "Undo History" },
  { "<space>uC", function() Snacks.picker.colorschemes() end, desc = "Colorschemes" },
  -- LSP
  { "gd", function() Snacks.picker.lsp_definitions() end, desc = "Goto Definition" },
  { "gD", function() Snacks.picker.lsp_declarations() end, desc = "Goto Declaration" },
  { "gr", function() Snacks.picker.lsp_references() end, nowait = true, desc = "References" },
  { "gi", function() Snacks.picker.lsp_implementations() end, desc = "Goto Implementation" },
  { "gy", function() Snacks.picker.lsp_type_definitions() end, desc = "Goto T[y]pe Definition" },
  { ",ss", function() Snacks.picker.lsp_symbols() end, desc = "LSP Symbols" },
  { ",sS", function() Snacks.picker.lsp_workspace_symbols() end, desc = "LSP Workspace Symbols" },
}
else
M =  {
    {",gg", function () Snacks.lazygit() end, desc = "Lazygit"},
    { "<space>n",  function() Snacks.notifier.show_history() end, desc = "Notification History" },
    { "<space>un", function() Snacks.notifier.hide() end, desc = "Dismiss All Notifications" },
    { ",gB", function() Snacks.gitbrowse() end, desc = "Git Browse", mode = { "n", "v" } },
    { ",gl", function() Snacks.lazygit.log() end, desc = "Lazygit Log (cwd)" },
    { ",gb", function() Snacks.git.blame_line() end, desc = "Git Blame Line" },
    -- { "tt", function() Snacks.terminal.toggle() end, desc = "Toggle a terminal window" },
}
end

return M
